package com.tekj.cxqcb.presenter.main;

import android.app.Activity;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;

import com.tekj.cxqcb.R;
import com.tekj.cxqcb.config.Urls;
import com.tekj.cxqcb.presenter.main2.Main2PresenterImpl;

import java.util.ArrayList;
import java.util.List;

import common.tool.AppUpdateManager;
import es.dmoral.toasty.Toasty;

/**
 * description 程序装载类
 * Created by JustTong on 2018/11/29.
 */
public class MainPresenterImpl implements MainPresenter {

    // 设置退出时长
    private long mExitTime;
    private Fragment currentShowFragment;//当前显示的模块
    private Activity mActivity;

    public MainPresenterImpl(Activity activity) {
        this.mActivity = activity;
    }


    @Override
    public void initcheckUpdate() {
//        new Handler().postDelayed(new Runnable() {
//            public void run() {
//                //自动检测版本升级
//                AppUpdateManager mAppUpdateManager = new AppUpdateManager(mActivity, Urls.AppUpdata, false);
//                mAppUpdateManager.checkUpdate();
//            }
//        }, 1000);


    }

    @Override
    public List<Fragment> getFragmentList() {
        List<Fragment> fragmentList = new ArrayList();
        //根据实际情况进行调整添加

        return fragmentList;
    }

    @Override
    public void onExit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toasty.normal(mActivity, "再按一次退出程序").show();
            mExitTime = System.currentTimeMillis();
        } else {
            mActivity.finish();
        }
    }

    @Override
    public void showFragment(FragmentManager supportFragmentManager, Fragment fragment) {

        FragmentManager fm = supportFragmentManager;
        FragmentTransaction transaction = fm.beginTransaction();

        if (currentShowFragment == fragment) {
            return;
        }

        if (currentShowFragment != null) {
            transaction.hide(currentShowFragment);
        }

        currentShowFragment = fragment;

        if (!currentShowFragment.isAdded()) {
            transaction.add(R.id.fl_main, currentShowFragment);
        }

        transaction.show(currentShowFragment);

        transaction.commit();
    }

    @Override
    public void initSetIconfont(TextView[] tvIco) {
        //初始化Iconfont
        Main2PresenterImpl mMain2PresenterImpl = new Main2PresenterImpl(mActivity);
        mMain2PresenterImpl.initTab(tvIco);

    }

    @Override
    public void initTabClick(TextView[] tvIco, TextView[] tvName, int index,FragmentManager supportFragmentManager) {
        //显示Fragment
        Fragment fragment = getFragmentList().get(index);
        showFragment(supportFragmentManager, fragment);

        //显示tab按钮
        Main2PresenterImpl mMain2PresenterImpl = new Main2PresenterImpl(mActivity);
        mMain2PresenterImpl.initTabColor(tvIco, tvName, index);

    }
}
